package com.example.oldtest;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.fail;

public class Adduser {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();

    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://localhost:8888/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void testadduser() throws Exception {
        driver.get(baseUrl + "/php4dvd/");
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys("admin");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("admin");
        driver.findElement(By.name("submit")).click();
        driver.findElement(By.linkText("User management")).click();
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys("guestuser");
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("12345@mail.ru");
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys("12345");
        driver.findElement(By.id("password2")).clear();
        driver.findElement(By.id("password2")).sendKeys("12345");
        driver.findElement(By.name("submit")).click();
        driver.findElement(By.id("username")).clear();
        driver.findElement(By.id("username")).sendKeys("Editoruser");
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys("54321@mail.ru");
        driver.findElement(By.id("password")).clear();
        driver.findElement(By.id("password")).sendKeys("54321");
        driver.findElement(By.id("password2")).clear();
        driver.findElement(By.id("password2")).sendKeys("54321");
        new Select(driver.findElement(By.name("permission"))).selectByVisibleText("Editor");
        driver.findElement(By.name("submit")).click();
        driver.findElement(By.linkText("Log out")).click();
        driver.switchTo().alert().accept();
        // ERROR: Caught exception [ERROR: Unsupported command [getConfirmation]]
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}
